<!--策略启动-自定义-->
<template>
  <div class="bg" ref="content">
    
    <VTips class="tips" v-if="datas && !datas.length" 
    :text="['当前暂无信号', '点击下方按钮创建吧～']" image="list_kong.png"
    :width="283" :height="238">
      <div class="row jcenter">
        <div class="tips-add main1" @click="addClick">
          <text class="font32 text1">{{T('创建信号')}}</text>
        </div>
      </div>
    </VTips>

    <template v-else>
      <list class="list" :loadmoreoffset="50" @loadmore="loadmore">
        <refresh class="refresh" @refresh="refresh" :display="isRefresh ? 'show' : 'hide'">
          <loading-indicator class="load text1"></loading-indicator>
        </refresh>

        <cell v-for="(item, index) in datas" v-bind:key="index">
          
          <SignalCellV v-if="item.signalName" 
          class="cell" :item="item"
          @up="upClick(item, index)" @end="endClick(item, index)"
          @func="funcClick($event, item, index)"/>

          <template v-else>
            <div class="genduo">
              <text class="font22 text3">{{T('没有更多数据')}}</text>
            </div>
            <div class="height104"></div>
          </template>
        </cell>
      </list>

      <div v-if="datas" class="add main-bt1" @click="addClick">
        <LImage class="img-add" :src="Config.img('xinhao.png')" />
        <text class="font32 text1">{{T('创建信号')}}</text>
      </div>
    </template>

    <!--弹出菜单-->
    <ACFunc ref="func" @func="funcSelect"/>
  </div>
</template>

<script>

//组件

//功能
import jmodal from "@/_js/modal";
import jconfig from "@/_js/config";
import jtime from "@/_js/time"
import jnavigator from "@/_js/navigator"; 
import jbroadcast from "@/_js/broadcast";
import jhttps from "@/_js/https";
import jSignal from "./Signal"

const weexFunction = weex.requireModule('WeexFunction');
const dom = weex.requireModule("dom");

import I18N from "@/_i18n/i18n";
var T = I18N.t;
weexFunction.setTitle(T('信号'));

import ACFunc from "@/_public/ACFunc";
import VTips from "@/_public/VTips"
import SignalCellV from "./SignalCellV";
import SignalCellT from "./SignalCellT";
export default {

  components: {
    SignalCellV,
    SignalCellT,
    ACFunc,
    VTips
  },
  
  data() {
    return {
      T:T,
      Config: jconfig,

      isRefresh:false,
      isLoadinging: false,
      pageMax:false,
      pageNo:1,
      pageSize:20,
      datas:undefined,
    };
  },

  mounted() {
    setTimeout(() => {
      this.refresh();
    }, 200);
  },
  
  methods: {
    /************************************************* 通信触发*/
    //完全显示
    viewappear() {
      this.refresh();
    },

    //完全隐藏
    viewdisappear() {
    },

    /************************************************* 其他*/
    refresh() {
      if (this.isRefresh) return;
      this.isRefresh = true;
      this.pageNo = 1;
      this.httpsGetSignalList();
    },

    //加载
    loadmore() {
      if (this.pageMax || this.isRefresh || this.isLoadinging) return;
      this.isLoadinging = true;
      this.httpsGetSignalList();
    },

    initDatas(datas) {

      datas.forEach((item)=>{
        item.createTimeStr = jtime.getTime(item.createTime);
        item.statesBol = item.states=='execute';

        try {
          var signalRecord = item.signalRecord;
          item.SignalTimeStr = jtime.getTime(signalRecord.createTime);
          item.SignalContentStr = signalRecord.content;
        } catch (error) {}
      });
      return datas;
    },

    /********************************************** https*/
    //https 信号列表
    httpsGetSignalList(){

      if(!jconfig.isLogin) {
        this.datas = [];
        this.pageMax = true;
        this.isRefresh = false;
        return;
      }

      var param = {"pageNo": this.pageNo, "pageSize": this.pageSize}
      jhttps.post("signal/getSignalList",param,true,(data)=>{
        
        this.isRefresh = false;
        if(data.status == 200){

          var datas = this.initDatas(data.data);
          if(this.pageNo == 1) {
            this.datas = datas;
          } else {
            this.datas.push(...datas);
          }

          this.pageMax = datas.length < this.pageSize;
          if (this.pageMax && this.datas.length) {
            this.datas.push({});
          }

          this.pageNo++;
        }else{;
          jmodal.toast(data.msg);
        }
      });
    },

    //https 启动信号
    httpSignalStart(item, server){

      jmodal.showLoad();
      var param = {'signalId': item.signalId, 'address': server.innerIp}
      jhttps.postX("signal/signalStart",param,true,(data)=>{
        jmodal.hideLoad();
        if(data.status == 200){
          item.statesBol = true;
          this.$forceUpdate()
          jmodal.toast('启动成功')
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    //https 停止信号
    httpsSignalEnd(item){

      jmodal.showLoad();
      var param = {'signalId': item.signalId}
      jhttps.postX("signal/signalEnd",param,true,(data)=>{

        jmodal.hideLoad();
        if(data.status == 200){
          item.statesBol = false;
          this.$forceUpdate()
          jmodal.toast('停止成功')
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    //https 删除信号
    httpsdDeleteSignalById(item, index){

      jmodal.showLoad();
      var param = {'signalId': item.signalId}
      jhttps.postX("signal/deleteSignalById",param,true,(data)=>{

        jmodal.hideLoad();
        if(data.status == 200){
          this.datas.splice(index, 1);
          this.datas = this.datas.length==1 ? [] : this.datas;
          jmodal.toast('删除成功')
        }else{
          jmodal.toast(data.msg);
        }
      });
    },

    /********************************** 点击*/
    //菜单点击
    menuClick(menu){
      if (this.menu == menu) return;
      this.menu = menu;
      this.datas = undefined;
      this.refresh();
    },

    //功能显示
    funcClick(event, item, index){

      var funcs = [T('编辑信号'), T('信号记录'), T('删除信号')];
      this.$refs.func.show(funcs, event.position, this.$refs.content, {item, index});
    },

    //功能选择
    funcSelect(item, index, data) {

      var param = {'signalId':data.item.signalId};
      if (item == T('编辑信号')) {
        jnavigator.open('', 'signal/SignalEditV', param);
        jbroadcast.subscribeOnce('SignalEdit', ()=>{
          this.refresh();
        });
      }

      if (item == T('信号记录')) {
        param['type'] = 'vtrading';
        jnavigator.open('', 'signal/SignalRecord', param);
      }


      if (item == T('删除信号')) {

        if (data.item.states == 'execute') {
          jmodal.toast(T('信号启动中无法删除，停止后再删除'));
          return;
        }

        var parm = {title:T("提示"),
                    message:T("确认删除信号")+" "+data.item.signalName+" ？",
                    okBtn:T("确认"), 
                    cancelBtn:T("取消")};
        jmodal.confirm(parm, (bool)=>{
          if (bool == "true") {
            this.httpsdDeleteSignalById(data.item, data.index);
          }
        });
      }
    },

    //启动
    upClick(item, index) {

      jSignal.showServers((server)=>{
        this.httpSignalStart(item, server)
      })
    },

    //停止
    endClick(item, index) {
      this.httpsSignalEnd(item);
    },

    //创建信号
    addClick() {
      if (!jconfig.forceLogin()) return;

      var array = [T('Vtrading信号'), T('TradingView自定义')];
      jmodal.actionSheet(array, '', (value)=>{

        var url;
        if (value == T('Vtrading信号')) {
          url = 'signal/SignalAddV';
        } else if (value == T('TradingView自定义')) {
          url = 'signal/SignalAddT';
        }
        jnavigator.open('', url);
        jbroadcast.subscribeOnce('SignalAdd', ()=>{
          this.refresh();
        });
      });
    }
  }
}
</script>

<style src="@/_css/style.css" ></style>
<style scoped>
.bg {
  flex: 1;
}
.list {
  flex: 1;
  margin-top: 30px;
}
.cell {
  margin-left: 30px;
  margin-right: 30px;
  margin-bottom: 30px;
}
.refresh {
  width: 750;
  flex-direction: row;
  justify-content: center;
}
.load {
  margin-top: 16px;
  height: 40px;
  width: 40px;
}
.genduo {
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 750px;
  height: 100px;
}

/*************** 启动*/
.add {
  position: absolute;
  bottom: 20px;
  right: 40px;

  flex-direction: row;
  align-items: center;
  justify-content: center;

  height: 84px;
  border-radius: 42px;
  padding-left: 20px;
  padding-right: 20px;
}
.img-add {
  width: 34px;
  height: 32px;
  margin-right: 10px;
}
.tips {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}
.tips-add{
  align-items: center;
  justify-content: center;
  width: 380px;
  height: 80px;
  background-color: #1677FE;
  border-radius: 40px;
  margin-top: 40px;
}

.menu {
  margin-top: 20px;
  margin-left: 40px;
}
/*************** 功能弹窗*/
.func{
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 9;
}
.func-frame {
  position: absolute;
  right: 30px;
}
.func-jiao {
  position: absolute;
  width: 28px;
  height: 20px;
  right: 50px;
}
.func-content {
  border-radius: 16px;
  
  padding-top: 20px;
  padding-bottom: 20px;
  padding-left: 40px;
  padding-right: 40px;

  margin-top: 20px;
}
.func-text {
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.server {
  width: 0px;
  height: 0px;
  overflow: hidden;
}

.height104 {
  height: 104px;
}
</style>